jQuery.fn.extend({
	//表单验证，封装基于Validform.js
	//$("form").validformIn(successFn); 
	validform: function(type) {
        $(this).each(function(i,o) {
            $(o).Validform({
                showAllError: true, //是否全部验证
                tiptype: function(msg, o, cssctl) { //提示类型, 自定义弹出提示
		            if(!o.obj.is(msg,"form")) {
		                layer.closeAll('tips');
		                if(o.type != 2) {
		                    msg = $(o.obj).attr("datatype") == 'file' ? '不能为空！' : msg;
		                    setTimeout(function(){
		                    	if(!$(o.obj).is(":visible")  || $(o.obj).css("display") =="none"){
		        					layer.msg("存在必填项为空！", {icon: 0,time:1500,offset: '25%'});
		        				}else{
		        					layer.tips(msg,$(o.obj),{
			                            tips: [1, '#f1355e'],
			                            time: 1500,
			                            fixed: false,
			                            tipsMore: true
			                        });
		        				}
		                    },10);
		                }
		            }
		            if(o.type==3){ window.validformInFlag = false;  }
		        },
                datatype:{
                    "chooseBox":function(gets,obj,curform,regxp){ //验证 chooseBox类型是否必填
                        var timoutide;
                        if($(obj).attr("data-value") && $(obj).children().length >0 || $(obj).find("a").length>0){
                            return true;
                        }else{
                            $(obj).unbind("DOMNodeInserted").bind('DOMNodeInserted', function() { 
                                clearInterval(timoutide);
                                timoutide = setTimeout(function(){  $(curform).validformIn() },10);
                            });
                            return false;
                        }
                    },
                    "file":function(gets,obj,curform,regxp){ //验证 chooseBox类型是否必填
                        var timoutide;
                        if($(obj).find(".fileUpload-link").text()!="相关附件(0)"){
                            return true;
                        }else{
                            $(obj).unbind("DOMNodeInserted").bind('DOMNodeInserted', function() { 
                                clearInterval(timoutide);
                                timoutide = setTimeout(function(){  $(curform).validformIn() },10);
                            });
                            return "附件不能为空！";
                        }
                    },
                    chkpwd:function(gets,obj,curform,regxp){
                    	if(!gets){ return false; }
                    	if( $(obj).val() != $(curform).find("[name='"+$(obj).attr("chkpwdfield")+"']").val() ){
                    		return "请保证两个密码一致！";
                    	}else{
                    		return true;
                    	}
                    }
                },
                callback: function(data) { //验证成功后是否提交表单！ 直接return false 就不提交
                    return false;
                }
            });
        });
	},
	validformIn: function(callback) {
		window.validformInFlag = true;
		$(this).validform();
		$(this).submit();
		if(window.validformInFlag == true) {
			if(callback) { callback($(this).serializeJson()); }
			return true;
		} else {
			return false;
		};
	},
	//提取form表单数据
	//$("div").serializeJson(); 			
	serializeJson: function() {
		var $th = this;
		var Input = $th.find("input");
		var Select = $th.find("select");
		var Textarea = $th.find("textarea");
		var Checkbox = $th.find("input[type='checkbox']");
		var Radio = $th.find("input[type='radio']");
		var Search = $th.find("div[type='search']");
		var obj = {};
		function treatmen(dom) {
			$.each(dom, function() {
				if(this.type == "button" || this.type == "checkbox" || this.type == "radio") {
				    return true;
				};
				if($(this).parents('.layui-table').length >0 ){
					return true;
				}
				if($(this).attr("form-datatype")=="number" && this.name){
				    obj[this.name] = Number($.delcommafy($.trim(this.value))); 
				}else if($(this).attr("type")=="number" && this.name){
				    obj[this.name] = Number($.trim(this.value)); 
				}else if(this.name){
				    obj[this.name] = $.trim(this.value); 
				}
			});
		};
		function twoTreatment(dom) {
			var names = $.unique( $.map(dom, function(h) {
			    if( $(h).attr("name")!="layTableCheckbox" && $(h).parents('.layui-table').length <1 ){
					return $(h).attr("name")
				}
			}) );
			$.each(names, function(index, o) {
				var box = $th.find("input[name='" + o + "']:checked");
				obj[o]=$.map(box, function(h) { return  $.trim(h.value)   }).join(',');
			});
		};
		function search(dom){
            $.each(dom, function() {
            	if($(this).parents('.layui-table').length >0 ){
					return true;
				}
                if($(this).attr("name")){
                    obj[$(this).attr("name")] = $.trim($(this).attr("data-value")); 
                }
            });
        }
		treatmen(Input);
		treatmen(Textarea);
		treatmen(Select);
		twoTreatment(Checkbox);
		twoTreatment(Radio);
		search(Search);
		
		//转义script标签
		return $.scriptEncode(obj);
	}
});